package com.mybook.app.service;

import com.github.pagehelper.PageInfo;
import com.mybook.app.core.UserContext;
import com.mybook.app.dto.BookInfoAndUserInfoDto;
import com.mybook.app.exception.DataIsEmptyException;
import com.mybook.app.mapper.*;
import com.mybook.app.model.*;
import com.mybook.app.request.BookDiscoverRequest;
import com.mybook.app.response.*;
import com.mybook.app.utils.LatLngUtils;
import com.mybook.app.utils.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zjutsw on 2017/10/30.
 */
@Service
public class BookService {
    @Autowired
    private HotListMapper hotListMapper;
    @Autowired
    private NewListMapper newListMapper;
    @Autowired
    private BookInfoMapper bookInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private CollectInfoMapper collectInfoMapper;
    @Autowired
    private UserService userService;

    /**
     * 获取新书推荐
     * 3本
     *
     * @return
     */
    public List<NewBook> recommendNew() {
        List<NewBook> newBooks = null;
        List<NewList> newLists = newListMapper.findNewBookTop3OrderByCTimeDesc();
        if (newLists.isEmpty()) {
            Logger.info(this, "查询数据为空");
            throw new DataIsEmptyException();
        }
        // 数据复制
        newBooks = newLists.stream().map((newList) -> {
            NewBook newBook = new NewBook();
            newBook.setbId(newList.getId());
            newBook.setName(newList.getName());
            newBook.setUrl1(newList.getUrl1());
            newBook.setIsLocal(newList.getIsLocal());
            return newBook;
        }).collect(Collectors.toList());
        return newBooks;
    }

    /**
     * 获取新书详情
     * 3本
     *
     * @return
     */
    public NewBookDetail detailNew(String id) {
        NewList newList = newListMapper.findNewBookById(id);
        if (null == newList) {
            Logger.info(this, "查询数据为空");
            throw new DataIsEmptyException();
        }
        // 数据复制
        NewBookDetail newBookDetail = new NewBookDetail();
        BeanUtils.copyProperties(newList, newBookDetail);
        return newBookDetail;
    }

    /**
     * 获取热书推荐
     * 9本
     *
     * @return
     */
    public List<HotBook> recommendHot() {
        List<HotBook> hotBooks = null;
        List<HotList> hotLists = hotListMapper.findHotBookTop9OrderByCTimeDesc();
        if (hotLists.isEmpty()) {
            Logger.info(this, "查询数据为空");
            throw new DataIsEmptyException();
        }
        // 数据复制
        hotBooks = hotLists.stream().map((hotList) -> {
            HotBook hotBook = new HotBook();
            hotBook.setbId(hotList.getId());
            hotBook.setName(hotList.getName());
            hotBook.setUrl1(hotList.getUrl1());
            hotBook.setAuthor(hotList.getAuthor());
            hotBook.setPress(hotList.getPress());
            //查询持有者名字
            UserInfo userInfo = userInfoMapper.findUserInfoById(hotList.getCuId());
            if (null != userInfo) {
                hotBook.setCuName(userInfo.getName());
            }
            return hotBook;
        }).collect(Collectors.toList());
        return hotBooks;
    }

    /**
     * 获取热书详情
     *
     * @return
     */
    public BookInfoAndUserInfoAndCollectInfo detailBook(String id) {

        BookInfoAndUserInfoDto bookInfoAndUserInfoDto = bookInfoMapper.findBookInfoByIdWithUserInfo(id);
        // 数据为空
        if (null == bookInfoAndUserInfoDto) {
            Logger.info(this, "查询书籍数据为空");
            throw new DataIsEmptyException();
        }

        //查询当前用户坐标
        String latLng = userService.getCoor();

        // 数据转换
        BookInfoAndUserInfoAndCollectInfo bookInfoAndUserInfo = new BookInfoAndUserInfoAndCollectInfo();
        bookInfoAndUserInfo.setBookInfoDetail(new BookInfoDetail());
        BeanUtils.copyProperties(bookInfoAndUserInfoDto, bookInfoAndUserInfo.getBookInfoDetail());
        if (null != bookInfoAndUserInfoDto.getCuUserInfo()) {
            bookInfoAndUserInfo.setCuUserInfo(new User());
            BeanUtils.copyProperties(bookInfoAndUserInfoDto.getCuUserInfo(), bookInfoAndUserInfo.getCuUserInfo());
            // 设置距离
            bookInfoAndUserInfo.getCuUserInfo().setDistance(LatLngUtils.getDistance(latLng, bookInfoAndUserInfoDto.getCuUserInfo().getCoor()));
        }
        if (null != bookInfoAndUserInfoDto.getOuUserInfo()) {
            bookInfoAndUserInfo.setOuUserInfo(new User());
            BeanUtils.copyProperties(bookInfoAndUserInfoDto.getOuUserInfo(), bookInfoAndUserInfo.getOuUserInfo());
            bookInfoAndUserInfo.getOuUserInfo().setDistance(LatLngUtils.getDistance(latLng, bookInfoAndUserInfoDto.getOuUserInfo().getCoor()));
        }

        //查询是否收藏
        CollectInfo collectInfo = collectInfoMapper.findCollectInfoByIdAndBId(UserContext.getCurrentuserId().get(), id);
        Collect collect = new Collect();
        if (null != collectInfo) {
            // 收藏
            collect.setFlag(true);
        } else {
            collect.setFlag(false);
        }

        bookInfoAndUserInfo.setCollect(collect);
        return bookInfoAndUserInfo;
    }


    /**
     * 图书发现
     *
     * @param bookDiscoverRequest
     */
    public PageInfo<BookInfoAndUserInfoDto> bookDiscover(BookDiscoverRequest bookDiscoverRequest) {
        // 查询
        List<BookInfoAndUserInfoDto> bookInfos = bookInfoMapper.findBookInfoByKeywordAndNearAndOrderAndSexAndPage(bookDiscoverRequest);
        //数据转换
        if (CollectionUtils.isEmpty(bookInfos)) {
            Logger.info(this, "查询书籍数据为空");
            throw new DataIsEmptyException();
        }

        //分页新信息
        PageInfo<BookInfoAndUserInfoDto> pageInfo = new PageInfo<BookInfoAndUserInfoDto>(bookInfos);
        return pageInfo;


    }

}
